/*
* ============================================================================
*  Name     : GV60ListContainer.cpp
*  Part of  : GV60
*  Copyright: Wipo Software
* ============================================================================
*/

// INCLUDE FILES
#include <aknlists.h>          // Listbox
#include <AknIconArray.h>      // IconList
#include <eikclbd.h>           // ColumnListBoxData (for IconList)
#include <AknsDrawUtils.h>     // skin
#include <AknsBasicBackgroundControlContext.h>  //skin 
#include <aknnotewrappers.h> 
#include <eikapp.h>

#include "GV60PhonebookContainer.h"
#include "IconFileProvider.h"
#include <GV60.rsg>
#include <GV60_listbox.mbg>

_LIT(KMifFileName, "GV60_listbox.mif");


// ================= MEMBER FUNCTIONS =======================
CGV60PhonebookContainer::CGV60PhonebookContainer(MEikListBoxObserver* n_pListBoxObserver)
{
  m_pListBoxObserver = n_pListBoxObserver;
  
  m_pListBox         = NULL;
  m_pBackGround      = NULL;
  m_pFileProvider    = NULL;
}

void CGV60PhonebookContainer::ConstructL(const TRect& aRect)
{
  CreateWindowL();

  // Create listbox
  m_pListBox = new (ELeave) CAknDoubleGraphicStyleListBox;
  m_pListBox->ConstructL(this, EAknListBoxSelectionList);
  m_pListBox->SetContainerWindowL( *this );
  m_pListBox->SetListBoxObserver( m_pListBoxObserver );
  m_pListBox->CreateScrollBarFrameL(ETrue);
  m_pListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto);

  CreateIconsL();
  CreateEmptyListL();

  SetRect(aRect);
  m_pBackGround = CAknsBasicBackgroundControlContext::NewL( KAknsIIDQsnBgAreaMain, Rect(), EFalse );
  ActivateL();
}

CGV60PhonebookContainer::~CGV60PhonebookContainer()
{
  if (m_pListBox)
  {
    delete m_pListBox;
    m_pListBox = NULL;
  }
  if (m_pBackGround)
  {
    delete m_pBackGround;
    m_pBackGround = NULL;
  }
  if (m_pFileProvider)
  {
    delete m_pFileProvider;
    m_pFileProvider = NULL;
  }
  m_iFsSession.Close();
}

void CGV60PhonebookContainer::SizeChanged()
{
  CCoeControl::SizeChanged();
  
  if (m_pListBox)
  {
    m_pListBox->SetRect( Rect() );
  }
}

void CGV60PhonebookContainer::HandleResourceChange(TInt aType)
{
  CCoeControl::HandleResourceChange(aType);

  if (aType == KEikDynamicLayoutVariantSwitch)
  {
    TRect iRect;
    AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, iRect);
    SetRect(iRect);
  }
}

TInt CGV60PhonebookContainer::CountComponentControls() const
{
  if (m_pListBox)
  {
    return 1;
  }
  return 0;
}

CCoeControl* CGV60PhonebookContainer::ComponentControl(TInt aIndex) const
{
  switch ( aIndex )
  {
    case 0:
      return m_pListBox;
    default:
      return NULL;
  }
}

void CGV60PhonebookContainer::Draw(const TRect& aRect) const
{
  CWindowGc& gc = SystemGc();

  // Skin drawing
  MAknsSkinInstance* skin = AknsUtils::SkinInstance();
  MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
  AknsDrawUtils::Background( skin, cc, this, gc, aRect );
}

TTypeUid::Ptr CGV60PhonebookContainer::MopSupplyObject(TTypeUid aId)
{
  if(aId.iUid == MAknsControlContext::ETypeId && m_pBackGround)
  {
    return MAknsControlContext::SupplyMopObject( aId, m_pBackGround);
  }
  return CCoeControl::MopSupplyObject( aId );
}

void CGV60PhonebookContainer::HandleControlEventL(CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
{
  // Nothing to do
}

TKeyResponse CGV60PhonebookContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
{
  if (m_pListBox)
  {
    return m_pListBox->OfferKeyEventL( aKeyEvent, aType );
  }
  return EKeyWasNotConsumed;
}

TInt CGV60PhonebookContainer::GetSelectedItem()
{
  if (m_pListBox)
  {
    return m_pListBox->CurrentItemIndex();
  }
  return -1;
}

void CGV60PhonebookContainer::CreateIconsL()
{
  CArrayPtr<CGulIcon>* pIconList = new (ELeave) CAknIconArray(2);
  CleanupStack::PushL(pIconList);

  User::LeaveIfError(m_iFsSession.Connect());

  // Let other sessions to use this. ShareProtected() must be called for
  // enabling other other threads in this process to use this handle.
  m_iFsSession.ShareProtected();

  TBuf<256> iPath;
  m_iFsSession.PrivatePath(iPath);

  iPath.Append(KMifFileName());
  // insert the drive to the private path
  TParsePtrC parse( (CEikonEnv::Static()->EikAppUi()->Application())->AppFullName() );
  iPath.Insert(0, parse.Drive() );

  CFbsBitmap* pNormalBitmap = NULL;
  CFbsBitmap* pNormalMask   = NULL;
  CGulIcon*   pNewIcon      = NULL;

  m_pFileProvider = CIconFileProvider::NewL(m_iFsSession, iPath);

  // Normal icon
  AknIconUtils::CreateIconL(pNormalBitmap, pNormalMask, *m_pFileProvider,
                            EMbmGv60_listboxListbox_contact,
                            EMbmGv60_listboxListbox_contact_mask);
  pNewIcon = CGulIcon::NewL(pNormalBitmap, pNormalMask);
  pIconList->AppendL(pNewIcon);  // transfer ownership

  m_pListBox->ItemDrawer()->ColumnData()->SetIconArray(pIconList);

  CleanupStack::Pop(pIconList);
}

void CGV60PhonebookContainer::CreateEmptyListL()
{
  // set items and ownership
  CDesCArray *itemList = new (ELeave) CDesCArrayFlat(10);
  m_pListBox->Model()->SetItemTextArray(itemList);
  m_pListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);

  // update listbox
  m_pListBox->HandleItemAdditionL();
}

void CGV60PhonebookContainer::UpdateListL()
{
  _LIT(KTab, "\t");
  
  CDesCArrayFlat* pTextItemArray = ((CDesCArrayFlat*)m_pListBox->Model()->ItemTextArray());
  User::LeaveIfNull(pTextItemArray);
  
  pTextItemArray->Reset();
  
  HBufC* pString = HBufC::New(100);
  User::LeaveIfNull(pString);
      
  for (TInt iCount = 0; iCount < 3; iCount++)
  {
    // Format: icon\tString1\tString2\t\t
    pString->Des().Copy(_L("0"));
    pString->Des().Append(KTab);
    pString->Des().Append(_L("Michele"));
    pString->Des().Append(KTab);
    pString->Des().Append(_L("Michele"));
    pString->Des().Append(KTab);
    pString->Des().Append(KTab);
      
    pTextItemArray->AppendL(*pString);
  }

  delete pString;

  m_pListBox->HandleItemAdditionL();
}


// End of File  


